package com.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.pojo.dto.roleUser.RoleUserAddDto;
import com.teacher.pojo.entity.RoleUser;
import com.teacher.mapper.RoleUserMapper;
import com.teacher.pojo.vo.roleUser.RoleUserGetVo;
import com.teacher.service.IRoleUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2022-08-12
 */
@Service
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUser> implements IRoleUserService {

    /**
     * 根据角色id查询所有用户
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult<List<RoleUserGetVo>> getUserByRoleId(Long id) {
        List<RoleUserGetVo> getVoList = baseMapper.getRoleUserGetVo(id);
        return CommonResult.success(getVoList);
    }

    /**
     * 更新角色教师表
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<String> up(RoleUserAddDto dto) {
        // 删除
        this.remove(new QueryWrapper<RoleUser>().lambda()
                .eq(RoleUser::getRoleId, dto.getId()));
        // 添加
        List<RoleUser> roleUserList = new ArrayList<>();
        for (String s : dto.getTeacherKeyList()) {
            RoleUser roleUser = new RoleUser();
            roleUser.setRoleId(dto.getId());
            roleUser.setTeacherKey(s);
            roleUserList.add(roleUser);
        }
        this.saveBatch(roleUserList);
        return CommonResult.success("操作成功");
    }
}
